﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Models;
using System.Data;
namespace BookStore.Controllers
{
    [HandleError]
    public class SidebarController : Controller
    {
        public ActionResult Index()
        {
            CategoriesModel tbCategory = new CategoriesModel();
            BillModel tbBill = new BillModel();
            BookModel tbBook = new BookModel();
            InformationModel tbInfo = new InformationModel();

            DataTable result = new DataTable();
            int i = 0;

            // get all category
            try
            {
                result = tbCategory.getAllCategories();
                if (result != null && result.Rows.Count > 0)
                {
                    string[][] category = new string[result.Rows.Count][];
                    i = 0;
                    foreach (DataRow row in result.Rows)
                    {
                        category[i] = new string[2];
                        category[i][0] = row["category_id"].ToString();
                        category[i][1] = row["category_name"].ToString();
                        i++;
                    }
                    ViewData["category"] = category;
                }
                else
                {
                    ViewData["category"] = null;
                }
            }
            catch (SystemException ex)
            {
                ViewData["category"] = null;
            }


            //get best seller
            result = new DataTable();
            i = 0;
            try
            {
                result = tbBill.getBestSeller();
                if (result != null && result.Rows.Count > 0)
                {
                    string[][] seller = new string[result.Rows.Count][];
                    string[] temp = new string[12];
                    i = 0;
                    foreach (DataRow row in result.Rows)
                    {
                        try
                        {
                            temp = tbBook.getBookbyID(Convert.ToInt32(row["book_id"]));
                        }
                        catch (SystemException ex)
                        {
                        }
                        finally
                        {
                            seller[i] = temp;
                            i++;
                        }
                    }
                    ViewData["seller"] = seller;
                }
                else
                {
                    ViewData["seller"] = null;
                }
            }
            catch (SystemException ex)
            {
                ViewData["seller"] = null;
            }


            result = new DataTable();
            // get about us
            try
            {
                result = tbInfo.getInformationById(1);
                if (result != null && result.Rows.Count > 0)
                {
                    DataRow row1 = result.Rows[0];
                    string[] about = new string[2];
                    about[0] = row1["name"].ToString();
                    about[1] = row1["description"].ToString();

                    ViewData["about"] = about;
                }
                else
                {
                    ViewData["about"] = null;
                }
            }
            catch (SystemException ex)
            {
                ViewData["about"] = null;
            }
            UsersModel user = new UsersModel();
          
            // get username
            if(Session["Session_User"]!=null)
            {
                try
                {
                      ViewData["user_name"] = user.getUserNamebyID1(Convert.ToInt32(Session["Session_User"]));
                    
                }
                catch (SystemException ex)
                {
                    ViewData["user_name"] = null;
                }
            }
            //get cart
            string[][] buy = (string[][])this.Session["buy"];
            if (buy != null)
            {
                string[] temp = new string[2];
                long money = 0;
                int total_book = 0;
                
                for (int j = 0; j < buy.Length; j++)
                {
                    money = money + Convert.ToInt64(buy[j][1]);
                    total_book += Convert.ToInt32(buy[j][2]);
                }
                temp[0] = total_book.ToString();
                temp[1] = money.ToString();
                ViewData["buy"] = temp;
            }
            else
            {
                ViewData["buy"] = null;
            }

            string[][] rent = (string[][])this.Session["rent"];
            if (rent != null)
            {
                string[] temp = new string[2];
                long money = 0;
                int total_book = 0;
                
                for (int j = 0; j < rent.Length; j++)
                {
                    money = money + Convert.ToInt64(rent[j][1]);
                    total_book += Convert.ToInt32(rent[j][2]);
                }
                temp[0] = total_book.ToString();
                temp[1] = money.ToString();
                ViewData["rent"] = temp;
            }
            else
            {
                ViewData["rent"] = null;
            }
            
            return View();
        }


    }
}
